package com.coolgor.coldot.config.web;


import com.coolgor.coldot.interceptor.auth.LoginInterceptor;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ComponentScan(basePackages = {"com.coolgor.coldot"})
public class WebMvcConfiguration implements WebMvcConfigurer {
	


    //spring boot中设置默认首页，当输入域名后自动跳转到默认指定的网页
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
//    	registry.addViewController("/").setViewName("forward:/index");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        WebMvcConfigurer.super.addViewControllers(registry);
    }
    
    
    //配置全局错误页面
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
            	
            	ErrorPage error400 = new ErrorPage(HttpStatus.BAD_REQUEST,"/error/400");
            	
            	ErrorPage error401 = new ErrorPage(HttpStatus.UNAUTHORIZED,"/error/401");

                ErrorPage error404 = new ErrorPage(HttpStatus.NOT_FOUND,"/error/404");

                ErrorPage error500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/error/404");
                
                factory.addErrorPages(error400,error401,error404,error500);
            }
        };
    }
    
    

    
    

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册拦截器 拦截规则
        //多个拦截器时 以此添加 执行顺序按添加顺序
        HandlerInterceptor loginInterceptor = new LoginInterceptor();
        List<String> loginInterceptorIncludePathPatterns = new ArrayList<String>();
        loginInterceptorIncludePathPatterns.add("/**");
        List<String> loginInterceptorExcludePathPatterns = new ArrayList<String>();
        loginInterceptorExcludePathPatterns.add("/");
        loginInterceptorExcludePathPatterns.add("/index*");
        loginInterceptorExcludePathPatterns.add("/public/**");//必须两个*，不然下层文件夹内的路径不得排除
        loginInterceptorExcludePathPatterns.add("/login/**");
        loginInterceptorExcludePathPatterns.add("/authlogin/*");
        loginInterceptorExcludePathPatterns.add("/wxqyhlogin/*");
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns(loginInterceptorIncludePathPatterns)
                .excludePathPatterns(loginInterceptorExcludePathPatterns);
    }

}